//
//  NCImageView.m
//  BaiduNews
//
//  Created by neusoft on 11-7-15.
//  Copyright 2011 Neusoft. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "NCemoticonViewPage.h"
#import "BtDefines.h"
#import "AQGridViewCell.h"
#import "EmoticonUtils.h"

#define kGridCellWidth      40
#define kGridCellHeight     45

#define kImageSize          30
#define kGridViewFame     CGRectMake(0,0,320,185)

@implementation NCEmoticonViewPage

#pragma mark -
#pragma mark Override base functions
- (id)initWithFaceArray:(NSArray*)faceTextArray withFrame:(CGRect)frame withDelegate:(id)delegate
{
    if ((self = [super initWithFrame:frame]))
    {
        _emoticonArray = [[NSArray alloc] initWithArray:faceTextArray];
        _gridView = [[AQGridView alloc] initWithFrame:kGridViewFame];
        _gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _gridView.autoresizesSubviews = YES;
        _gridView.delegate = self;
        _gridView.dataSource = self;
        _gridView.leftContentInset = 20;
        _gridView.scrollEnabled = NO;
        
        _delegate = delegate;
        
        [self addSubview:_gridView];
        [_gridView reloadData];
    }
    
    return self;
}



#pragma mark -
#pragma mark Grid View Data Source
- (NSUInteger) numberOfItemsInGridView: (AQGridView *) aGridView
{
    return [_emoticonArray count];
}

- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * emoticonViewPageViewCellIdentifier = @"EmoticonViewPageCell";
    
    AQGridViewCell *cell = (AQGridViewCell *)[gridView dequeueReusableCellWithIdentifier: emoticonViewPageViewCellIdentifier];
	if (cell == nil)
	{
		cell = [[[AQGridViewCell alloc] initWithFrame:CGRectMake(0.0, 
																  0.0, 
																  kGridCellWidth, 
																  kGridCellHeight)
									   reuseIdentifier:emoticonViewPageViewCellIdentifier] autorelease];
		cell.selectionStyle = AQGridViewCellSelectionStyleBlue;
	}
	
    NSString *emoticonText = [_emoticonArray objectAtIndex:index];
    NSString *imageName = [EmoticonUtils getEmoticonFileNameByText:emoticonText];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    //这里不再进行缩放
    //imageView.frame = CGRectMake(0, 0, kImageSize, kImageSize);
	[cell.contentView addSubview: imageView];
    return cell;
}

- (CGSize) portraitGridCellSizeForGridView: (AQGridView *) aGridView
{
    return ( CGSizeMake(kGridCellWidth, kGridCellHeight) );
}

#pragma mark -
#pragma mark Grid View Delegate
- (void) gridView: (AQGridView *) gridView didSelectItemAtIndex: (NSUInteger) index
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(selectEmoticon:)])
	{
        NSString *emoticonText = [_emoticonArray objectAtIndex:index];
		[_delegate selectEmoticon:emoticonText];
	}
    [_gridView deselectItemAtIndex:index animated:YES];
}

- (void)dealloc {
    [_emoticonArray release];
    [_gridView release];
    [super dealloc];
}


@end
